# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2019/5/26 18:07
# @Author  : wenjxu
# @Email   : wenjxususu@163.com
# @File    : spell.py
# @Software: PyCharm
import sys, optparse, os, logging
from core.conf.config import settings

sys.path.append(settings.BASE_DIR)  # 添加项目环境变量

from core.harTocase.harTocase.core import HarParser


class Main(object):
    def __init__(self):
        self.parser = optparse.OptionParser()
        # self.parser.add_option(
        #     "-c", "--clear", dest="clear", help="clear reports")
        self.parser.add_option(
            "-a", "--testapi", dest="testapi", help="generate testapi")
        self.parser.add_option(
            "-t", "--testcase", dest="testcase", help="generate testcase")
        # self.parser.add_option(
        #     "-r", "--run", dest="run", help="run json_file")

        self.options, self.args = self.parser.parse_args()  # options是对象，args是List
        logging.basicConfig(level="DEBUG")

        if getattr(self.options, "testapi"):  # 根据har文件生成api文件
            logging.info("Start to generate testapi.")
            if self.options.testapi and "har" in self.options.testapi:
                harfile_list = self.options.testapi.split(",")
                for harfile in harfile_list:
                    HarParser(harfile).gen_testapi_yaml()

        if getattr(self.options, "testcase"):  # 根据har文件生成testcase文件
            logging.info("Start to generate testcase.")
            if self.options.testcase and "har" in self.options.testcase:
                harfile_list = self.options.testcase.split(",")
                for harfile in harfile_list:
                    HarParser(harfile).gen_testcase_json()

if __name__ == '__main__':
    Main()
